/*
Dependencies: prototype.js, scriptaculous.js
*/
function Message(content, interval, options, contentType) {

	/**
	action = copy means that the html inside the node (message) is copy to a div element before display.
	action = cut means that the html inside the node (message) is cut and pasted to a div element before display.
	
	action only available if the variable content is a HTML element.
	*/
	if (!contentType) contentType = 'string';
	
	if (contentType == 'string') this.content = content;
	else if (contentType == 'element') {
		this.content = content.innerHTML;
		if (options && options.action == 'cut') content.innerHTML = '';
		if (options && options.copyback) this.originalNode = content;
	}
	
	this.interval = interval;
	this.divElement = null;
	if (options) this.options = options;
	
	if (options && options.onShow) this.onShow = this._onShow.bind(this);
	if (options && options.onHide) this.onHide = this._onHide.bind(this);
}

Message.prototype._onShow = function() {
	this.options.onShow();
}

Message.prototype._onHide = function() {
	this.options.onHide();
}

Message.prototype.destroy = function() {
	if (this.options && this.options.copyback) this.originalNode.innerHTML = this.divElement.innerHTML;
	this.divElement.innerHTML = '';
	this.divElement.remove();
	this.divElement = null;
	this.content = null;
	this.interval = null;
	this.options = null;
	this.onShow = null;
	this.onHide = null;
}

function MessageDisplayer(options) {
	this.msgList = new Array();
	
	this.wrapper = "announcement_wrapper";
	if (options && options.wrapper) this.wrapper = options.wrapper;
	
	this.mode = "sequential";
	if (options && options.mode) this.mode = options.mode;
	
	this.numShowing = 0;
	this.curIndex = 0;
}

MessageDisplayer.prototype.addMessage = function(message) {
	this.msgList.push(message);
	message.index = this.msgList.length-1;
	if (this.mode == "sequential" && this.numShowing == 0 || this.mode != "sequential") {
		this.showMessage(this.curIndex);
	}
}

MessageDisplayer.prototype.showMessage = function(index) {
	this.numShowing++;
	this.curIndex++;
	var wrapper = $(this.wrapper);
	var div = document.createElement("div");
	var message = this.msgList[index];
	div.className = "announcement";
	div.innerHTML = message.content;
	div.style.display = "none";
	div.id = this.wrapper+"_message_"+index;
	message.divElement = div;
	wrapper.appendChild(div);
	Effect.Appear(div.id);
	
	if (message.interval && message.interval > 0) {
		message.timeOutId = window.setTimeout(this.hideMessage.bind(this, index), message.interval);
	}
	if (message.onShow) message.onShow();
}

MessageDisplayer.prototype.hideMessage = function(index) {
	var message; 
	if (!index || index < 0) index = this.curIndex-1;
	if (index instanceof Message) message = index;
	else message = this.msgList[index];
	
	Effect.SwitchOff(message.divElement.id);
	if (message.timeOutId) {
		window.clearTimeout(message.timeOutId);
		message.timeOutId = null;
	}
	if (message.onHide) message.onHide();
	message.destroy();
	this.numShowing--;

	if (this.mode == "sequential") {
		if (this.curIndex < this.msgList.length && this.numShowing == 0) this.showMessage(this.curIndex);
	}
}

MessageDisplayer.prototype.getCurrentContentId = function() {
	var index = this.curIndex-1;
	return this.msgList[index].divElement.id;
}

MessageDisplayer.prototype.getNumMessageShowing = function() {
	return this.numShowing;
}